package com.yang.controller;



import com.yang.domain.DTO.AddRoleDto;
import com.yang.domain.DTO.ChangeRoleStatusDto;
import com.yang.domain.DTO.UpdateRoleDto;
import com.yang.domain.Result;
import com.yang.domain.VO.PageVo;
import com.yang.domain.entity.Role;
import com.yang.enums.AppHttpCodeEnum;
import com.yang.service.RoleService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 后台角色控制层
 * </p>
 * @author yang
 * @date 2023/5/29
 */
@RestController
@RequestMapping("/system/role")
public class RoleController {

    @Resource
    private RoleService roleService;


    @GetMapping("/list")
    public Result list(Role role, Integer pageNum, Integer pageSize){
        PageVo pageVo = roleService.selectRolePage(role,pageNum,pageSize);
        return Result.okResult(pageVo);
    }

    @PutMapping("/changeStatus")
    public Result changeStatus(@RequestBody ChangeRoleStatusDto dto){
        return roleService.changeStatus(dto);
    }


    @PostMapping
    public Result addRole(@RequestBody Role role){
        return roleService.addRole(role);
    }

    /**
     * 根据角色编号获取详细信息
     */
    @GetMapping(value = "/{roleId}")
    public Result getInfo(@PathVariable Long roleId) {

        Role role = roleService.getById(roleId);

        return Result.okResult(role);
    }

    /**
     * 修改保存角色
     */
    @PutMapping
    public Result edit(@RequestBody UpdateRoleDto updateRoleDto) {
        return roleService.updateRole(updateRoleDto);
    }

    @DeleteMapping("/{id}")
    public Result removeRole(@PathVariable(name = "id") Long id){

        boolean success = roleService.removeById(id);

        if (!success){
            return Result.errorResult(AppHttpCodeEnum.NO_SUCH_ROLE);
        }

        return Result.okResult();
    }

    @GetMapping("/listAllRole")
    public Result listAllRole(){
        List<Role> roles = roleService.selectRoleAll();
        return Result.okResult(roles);
    }

}
